from itsdangerous import TimedJSONWebSignatureSerializer as TJWSS

from meiduo_mall import settings


def generate_access_token(username):

    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=60)
    token = s.dumps({'username': username}).decode()
    return token


def decode_access_token(token):
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=60)

    try:
        data = s.loads(token)
        username = data.get('username')
        return username
    except:
        return None